package com.loan.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.loan.entity.Company;
import com.loan.entity.Information;
import com.loan.entity.Legal;
import com.loan.entity.Result;
import com.loan.mapper.CompanyMapper;
import com.loan.mapper.InformationMapper;
import com.loan.mapper.LegalMapper;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author
 * @since 2021-09-28
 */
@RestController
@RequestMapping("/information")
public class InformationController {


    @Resource
    RedisTemplate redisTemplate;

    @Autowired
    InformationMapper informationMapper;

    @Autowired
    CompanyMapper companyMapper;

    @Autowired
    LegalMapper legalMapper;

    @RequestMapping(value = "/getInformationList",method = RequestMethod.POST)
    public List<Information> getRateList(){
        List<Information> informationList = informationMapper.selectList(null);
        return informationList;
    }

    @RequestMapping(value = "/getInformationListByuid",method = RequestMethod.POST)
    public List<Information> getInformationListByuid(){
        Object uids = redisTemplate.opsForValue().get("uid");
        Integer uid = (Integer) uids;

        QueryWrapper<Information> wrapper=new QueryWrapper<>();
        wrapper.eq("uid",uid);
        List<Information> informationList = informationMapper.selectList(wrapper);
        return informationList;
    }

    @PostMapping("/addInformation")
    public Result addInformation(String way,String paymoney,String sumMoney,String nowMoney){

        Date date = new Date();
        String str = "yyy-MM-dd HH:mm:ss";
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(str);
        String df = simpleDateFormat.format(date);

        Object uids = redisTemplate.opsForValue().get("uid");
        Integer uid = (Integer) uids;
        //sumMoney:'',//总还款金额
        //nowMoney:''//之前已还款金额
        //paymoney:'',//现在的还款金额
        float sumMoneys= Float.parseFloat(sumMoney);
        float nowMoneys= Float.parseFloat(nowMoney);
        float paymoneys= Float.parseFloat(paymoney);
        float lastMoney = sumMoneys-nowMoneys-paymoneys;
        float zhifu=nowMoneys+paymoneys;


        //更新
        String zhifus= String.valueOf(zhifu);
        String zhifuss= String.valueOf(lastMoney);
        Legal legal = new Legal();
        legal.setPayMoney(zhifus);
        legal.setLastMoney(zhifuss);
        QueryWrapper<Legal> wrapper=new QueryWrapper<Legal>();
        wrapper.eq("uid",uid);
        legalMapper.update(legal,wrapper);

        //公司名字
        Company company=new Company();
        Map<String,Object> columnMap=new HashMap<>();
        columnMap.put("uid",uid);
        List<Company> companyList = companyMapper.selectByMap(columnMap);
        company = companyList.get(0);

        Information information = new Information();
        information.setUid(uid);
        information.setPaymoney(paymoney);
        information.setWay(way);
        information.setLastmoney(String.valueOf(lastMoney));
        information.setCompany(company.getCompanyFullName());
        information.setToday(new Date());

        int count=informationMapper.insert(information);

        Result result = new Result();
        if (count>0){
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        return result;
    }


}

